import { Message, MessageBox } from 'element-ui'
const _duration = 5 * 1000 // 5 秒

export const showSuccess = (msg, html, duration) => {
  message('success', msg, html, duration)
  // Message({
  //   message: msg,
  //   type: 'success',
  //   duration: duration || _duration,
  //   dangerouslyUseHTMLString: html
  // })
}

export const showWarning = (msg, html, duration) => {
  message('warning', msg, html, duration)
  // Message({
  //   message: msg,
  //   type: 'success',
  //   duration: duration || _duration,
  //   dangerouslyUseHTMLString: html
  // })
}

export const showError = (msg, html, duration) => {
  message('error', msg, html, duration)
  // Message({
  //   message: msg,
  //   type: 'error',
  //   duration: duration || _duration,
  //   dangerouslyUseHTMLString: html
  // })
}

export const message = (type, msg, html, duration) => {
  Message({
    message: msg,
    type: type,
    duration: duration || _duration,
    dangerouslyUseHTMLString: html
  })
}

export default function(Vue, options) {
  Vue.prototype.$success = function(msg, html) {
    showSuccess(msg, html)
  }

  Vue.prototype.$error = function(msg, html) {
    showError(msg, html)
  }

  Vue.prototype.$warn = function(msg, html) {
    showWarning(msg, html)
  }

  Vue.prototype.$alert = function(msg, cfg) {
    cfg = cfg || {}
    MessageBox.confirm(msg, cfg.title || '提示', {
      confirmButtonText: cfg.okText || '确定',
      showCancelButton: cfg.cancelText !== undefined,
      cancelButtonText: cfg.cancelText,
      type: cfg.type || ''
    }).then(() => {
      if (cfg.onOK) {
        cfg.onOK()
      }
    })
  }
}
